﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * You are given the following information, but you may prefer to do some research for yourself.

    1 Jan 1900 was a Monday.
    Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
    A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

     * 
     * 
     * */
    class Problem19
    {
        static Dictionary<int, int> daysPerMonth = new Dictionary<int, int>()
        {
            {1,31},
            {2,28},
            {3,31},
            {4,30},
            {5,31},
            {6,30},
            {7,31},
            {8,31},
            {9,30},
            {10,31},
            {11,30},
            {12,31}
        };

        public static string Calculate()
        {
            int year = 1901;
            int month = 1;

            int dayOfWeek = 1;

            int sundayCount = 0;

            while (year < 2001)
            {
                dayOfWeek += daysPerMonth[month];

                if (month == 2)
                    if (year % 100 == 0)
                    {
                        if (year % 400 == 0)
                            dayOfWeek++;
                    }
                    else if (year % 4 == 0)
                        dayOfWeek++;
                dayOfWeek %= 7;

                if (dayOfWeek == 6)
                    sundayCount++;

                month++;
                if (month > 12)
                {
                    year++;
                    month = 1;
                }
            }


            return sundayCount.ToString();
        }
    }
}
